package com.licheedev.myapplication;

import android.annotation.SuppressLint;
import com.licheedev.adplayer.data.AdData;
import com.licheedev.adplayer.data.AdDataHelper;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import org.apache.commons.lang3.StringUtils;

public class MyUrlAdData implements AdData {

    protected final URI mURI;
    protected final int mType;

    public MyUrlAdData(String url) {

        mURI = url == null ? URI.create("http://null") : AdDataHelper.fromUrl(url);

        if (isImage(url)) {
            mType = TYPE_IMAGE;
        } else {
            mType = TYPE_VIDEO;
        }
    }

    @Override
    public URI getURI() {
        return mURI;
    }

    @Override
    public int getType() {
        return mType;
    }

    @Override
    public long duration() {
        return 0;
    }

    public static boolean isImage(String url) {
        return StringUtils.containsIgnoreCase(url, ".jpg") //
            || StringUtils.containsIgnoreCase(url, ".jpeg") //
            || StringUtils.containsIgnoreCase(url, ".png") //
            || StringUtils.containsIgnoreCase(url, ".bmp") //
            || StringUtils.containsIgnoreCase(url, ".gif");
    }

    public static boolean isVideo(String url) {
        return StringUtils.containsIgnoreCase(url, ".mp4") //
            || StringUtils.containsIgnoreCase(url, ".avi") //
            || StringUtils.containsIgnoreCase(url, ".wmv") //
            ;
    }

    public static boolean isMusic(String url) {
        return StringUtils.containsIgnoreCase(url, ".mp3") //
            || StringUtils.containsIgnoreCase(url, ".aac") //
            || StringUtils.containsIgnoreCase(url, ".wma") //
            || StringUtils.containsIgnoreCase(url, ".wav") //
            || StringUtils.containsIgnoreCase(url, ".flac") //
            || StringUtils.containsIgnoreCase(url, ".ogg") //
            ;
    }

    public static List<MyUrlAdData> convert(List<String> urls) {
        if (urls == null || urls.size() < 1) {
            return new ArrayList<>();
        }

        ArrayList<MyUrlAdData> data = new ArrayList<>();
        for (String url : urls) {
            data.add(new MyUrlAdData(url));
        }

        return data;
    }

    public static List<MyUrlAdData> convert(String[] urls) {
        if (urls == null || urls.length < 1) {
            return new ArrayList<>();
        }

        ArrayList<MyUrlAdData> data = new ArrayList<>();
        for (String url : urls) {
            data.add(new MyUrlAdData(url));
        }

        return data;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        MyUrlAdData data = (MyUrlAdData) o;
        return mType == data.mType && mURI.equals(data.mURI);
    }

    @SuppressLint("NewApi")
    @Override
    public int hashCode() {
        return Objects.hash(mURI, mType);
    }
}
